﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

namespace Utility.Math
{
	public static class Vector3EX
	{
		public static bool IsAlmostZero (this Vector3 vec, float trashhold = 0.01f)
		{
			return vec.sqrMagnitude <= trashhold * trashhold;
		}

		public static Vector2 XY (this Vector3 vec3)
		{
			return new Vector2 (vec3.x, vec3.y);
		}

		public static Vector2 XZ (this Vector3 vec3)
		{
			return new Vector2 (vec3.x, vec3.z);
		}

		public static Vector2 YZ (this Vector3 vec3)
		{
			return new Vector2 (vec3.y, vec3.z);
		}

		public static float HorizontalAngleTo (this Vector3 from, Vector3 forward, Vector3 to)
		{
			if (from == to)
			{
				return 0;
			}

			Vector3 lookVec = to - from;
			var q_rotation = Quaternion.FromToRotation (forward, lookVec);
			float d_angle = q_rotation.eulerAngles.y;
			//angel between target dir and forward, in (-180, 180]
			d_angle = d_angle > 180 ? d_angle - 360 : d_angle;
			return d_angle;
		}

		public static Vector3 SetX (this Vector3 vec3, float value)
		{
			return new Vector3 (value, vec3.y, vec3.z);
		}
		public static Vector3 SetY (this Vector3 vec3, float value)
		{
			return new Vector3 (vec3.x, value, vec3.z);
		}
		public static Vector3 SetZ (this Vector3 vec3, float value)
		{
			return new Vector3 (vec3.x, vec3.y, value);
		}
	}
}